﻿// Decompiled with JetBrains decompiler
// Type: MCM.Implementation.XmlSettingsFormat
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using BUTR.DependencyInjection;
using BUTR.DependencyInjection.Logger;
using MCM.Abstractions.Base;
using MCM.Abstractions.GameFeatures;
using MCM.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;

#nullable enable
namespace MCM.Implementation
{
  public sealed class XmlSettingsFormat : BaseJsonSettingsFormat
  {
    public override IEnumerable<string> FormatTypes
    {
      get => (IEnumerable<string>) new string[1]{ "xml" };
    }

    public XmlSettingsFormat(IBUTRLogger<XmlSettingsFormat> logger)
      : base((IBUTRLogger) logger)
    {
    }

    public override bool Save(BaseSettings settings, GameDirectory directory, string filename)
    {
      IFileSystemProvider service = GenericServiceProvider.GetService<IFileSystemProvider>();
      if (service == null)
        return false;
      GameFile file = service.GetOrCreateFile(directory, filename + ".xml");
      if (file == null)
        return false;
      string str = this.SaveJson(settings);
      string name;
      if (settings is IWrapper wrapper)
      {
        object obj = wrapper.Object;
        if (obj != null)
        {
          name = obj.GetType().Name;
          goto label_8;
        }
      }
      name = settings.GetType().Name;
label_8:
      XmlDocument xmlDocument = JsonConvert.DeserializeXmlNode(str, name);
      if (xmlDocument == null)
        return false;
      using (MemoryStream memoryStream = new MemoryStream())
      {
        using (StreamWriter writer = new StreamWriter((Stream) memoryStream))
        {
          xmlDocument.Save((TextWriter) writer);
          try
          {
            return service.WriteData(file, memoryStream.ToArray());
          }
          catch (Exception ex)
          {
            return false;
          }
        }
      }
    }

    public override BaseSettings Load(
      BaseSettings settings,
      GameDirectory directory,
      string filename)
    {
      IFileSystemProvider service = GenericServiceProvider.GetService<IFileSystemProvider>();
      if (service == null)
        return settings;
      GameFile file = service.GetFile(directory, filename + ".xml");
      if (file == null)
        return settings;
      byte[] buffer = service.ReadData(file);
      if (buffer == null)
      {
        this.Save(settings, directory, filename);
        return settings;
      }
      XmlDocument xmlDocument = new XmlDocument();
      using (MemoryStream inStream = new MemoryStream(buffer))
      {
        xmlDocument.Load((Stream) inStream);
        XmlElement node = xmlDocument[settings.GetType().Name];
        if (node == null)
        {
          this.Save(settings, directory, filename);
          return settings;
        }
        string content = JsonConvert.SerializeXmlNode((XmlNode) node, Newtonsoft.Json.Formatting.None, true);
        if (!this.TryLoadFromJson(ref settings, content))
          this.Save(settings, directory, filename);
        return settings;
      }
    }
  }
}
